import axios from "axios";


const request = axios.create({
    baseURL:'/api',
    timeout: 50000,
    withCredentials:true,

})
// 请求拦截器
request.interceptors.request.use(
    (config) => {
      const token = localStorage.getItem("token");  // 在localStorage中找token这个值
      if (token) {
        config.headers.Authorization = `Bearer ${token}`; // 加入 Authorization 头
      }
      return config;
    },
    (error) => {
      return Promise.reject(error);
    }
  );

  // 响应拦截器
request.interceptors.response.use(
  (response) => {
      return response.data;
  },
  (error) => {
      console.log("错误", error)
      if (error.response && error.response.status === 401) {
          localStorage.removeItem('token'); // 清除无效Token

          ElMessage.warning("token过期, 请重新登录")
          // window.location.href = '/login'; // 跳转登录页
      }
      return Promise.reject(error);
  }
);



export default request